package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmLookup;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmLookupItemMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmLookupMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称：yusp-admin
 * @类名称：AdminSmLookupService
 * @类描述：数据字典类别目录
 * @功能描述:
 * @创建人：liaoxd@yusys.com.cn @创建时间：2017-12-13 11:18 @修改备注：
 * @修改日期 修改人员 修改原因 -------- -------- ----------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmLookupService extends CommonService {
	@Autowired
	private AdminSmLookupMapper mapper;
	@Autowired
	private AdminSmLookupItemMapper itemMapper;

	@Override
	protected CommonMapper<?> getMapper() {
		return this.mapper;
	}

	@Override
	public int insert(Object record) {
		AdminSmLookup adminSmLookup = (AdminSmLookup) record;
		adminSmLookup.setLastChgDt(DateUtil.formatDateTimeByDef());
		
	
		Map<String,String> map = new HashMap<String,String>();
		map.put("lookupCode", adminSmLookup.getLookupCode());
		List<?> list = this.mapper.getLookupByTypeCode(map);
		if(list != null && list.size() > 0 ) {
			return 0 ;
		}else {
			return this.insert(getMapper(), record);
		}	
	}
	
	@Override
	public int updateSelective(Object record) {
		AdminSmLookup adminSmLookup = (AdminSmLookup) record;
		adminSmLookup.setLastChgDt(DateUtil.formatDateTimeByDef());
		//adminSmLookup.setLastChgUsr(SecurityUtils.getCurrentUserLogin());
		return this.updateSelective(getMapper(), record);
	}

	/**
     * @函数名称:selectByModel
     * @函数描述:重新，支持页面模糊查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Override
    @SuppressWarnings("unchecked")
	public <T> List<T> selectByModel(QueryModel model) {
		if (model.getCondition().containsKey("lookupName")) {
			model.getCondition().put("lookupName", "%" + model.getCondition().get("lookupName") + "%");
		}
		// 设置分页查询参数(设置到线程变量中了)
		PageHelper.startPage(model.getPage(), model.getSize());
		List<Map<String,String>> list= this.mapper.getLookupListBycodeOrName(model);
		// 清除线程绑定的分页参数，防止影响同线程的其他查询
		PageHelper.clearPage();
		return (List<T>) list;
		
	}
	
	/**
     * @函数名称:getLookupByTypeCode
     * @函数描述:类别重复性校验查询
     * @参数与返回说明:
     * @算法描述:
     */
	public int getLookupByTypeCode(String lookupCode) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("lookupCode", lookupCode);
		int count = 0;
		
		if(this.mapper.getLookupByTypeCode(map) != null && this.mapper.getLookupByTypeCode(map).size() > 0) {
			count = this.mapper.getLookupByTypeCode(map).size();
		}
		return count;
	}
	
	
	@Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
	public int deleteByLookupCode(String id) {
		AdminSmLookup record = new AdminSmLookup();
		record.setLookupId(id);
		AdminSmLookup adminSmLookup = this.selectByPrimaryKey(record);
		int result = this.deleteByPrimaryKey(id);
		Map<String, String> map = new HashMap<String, String>();
		map.put("lookupCode", adminSmLookup.getLookupCode());
		this.itemMapper.deleteByLookupCode(map);
		return result;
	}
}
